home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / c-tools-.000 / c-tools- / c-tools-0.4 / cinfolib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-13  |  10.7 KB  |  614 lines

  1. /* $Id: cinfolib.c,v 1.23 1995/08/11 12:39:30 sandro Exp $ */
  2.  
  3. /* This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.  
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.  
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #include "misc.h"
  23. #include "cinfolib.h"
  24.  
  25. #if 0
  26. #define COMPACT_SEPARATOR '$'
  27. #else
  28. #define COMPACT_SEPARATOR '\1'
  29. #endif
  30.  
  31. char *
  32. cinfo_compact_info (char *output, char *i1, char *i2, char *i3, char *i4)
  33. {
  34.     char *p = output;
  35.     int num = 1;
  36.     if (i2 != NULL)
  37.     num++;
  38.     if (i3 != NULL)
  39.     num++;
  40.     if (i4 != NULL)
  41.     num++;
  42. #if 0
  43.     *p++ = num + '0';
  44. #else
  45.     *p++ = num;
  46. #endif
  47.  
  48.     while (*i1)
  49.     *p++ = *i1++;
  50.  
  51.     if (num > 1)
  52.     {    
  53.     *p++ = COMPACT_SEPARATOR;
  54.     while (*i2)
  55.         *p++ = *i2++;
  56.  
  57.     if (num > 2)
  58.     {
  59.         *p++ = COMPACT_SEPARATOR;
  60.         while (*i3)
  61.         *p++ = *i3++;
  62.  
  63.         if (num > 3)
  64.         {
  65.         *p++ = COMPACT_SEPARATOR;
  66.         while (*i4)
  67.             *p++ = *i4++;
  68.         }
  69.     }
  70.     }
  71.     *p++ = '\0';
  72.     return output;
  73. }
  74.  
  75. void
  76. cinfo_decompact_info (char *info, char *i1, char *i2, char *i3, char *i4)
  77. {
  78.     int level;
  79.     char *p = info;
  80.  
  81. #if 0
  82.     level = *p - '0';
  83. #else
  84.     level = *p;
  85. #endif
  86.  
  87.     *i1 = '\0';
  88.  
  89.     if (level > 1)
  90.     *i2 = '\0';
  91.     if (level > 2)
  92.     *i3 = '\0';
  93.     if (level > 3)
  94.     *i4 = '\0';
  95.  
  96.     while (*++p && *p != COMPACT_SEPARATOR)
  97.     *i1++ = *p;
  98.     *i1++ = 0;
  99.  
  100.     if (level > 1)
  101.     {
  102.     while (*++p && *p != COMPACT_SEPARATOR)
  103.         *i2++ = *p;
  104.     *i2++ = 0;
  105.     }
  106.  
  107.     if (level > 2)
  108.     {
  109.     while (*++p && *p != COMPACT_SEPARATOR)
  110.         *i3++ = *p;
  111.     *i3++ = 0;
  112.     }
  113.  
  114.     if (level > 3)
  115.     {
  116.     while (*++p && *p != COMPACT_SEPARATOR)
  117.         *i4++ = *p;
  118.     *i4++ = 0;
  119.     }
  120. }
  121.  
  122. char *
  123. cinfo_decompact_info_first (char *info, char *i1)
  124. {
  125.     char *p = info, *r = i1;
  126.  
  127.     *i1 = '\0';
  128.  
  129.      while (*++p && *p != COMPACT_SEPARATOR)
  130.     *i1++ = *p;
  131.     *i1++ = 0;
  132.  
  133.     return r;
  134. }
  135.  
  136. struct symbol_entry *
  137. cinfo_build_symbol_entry (char *name)
  138. {
  139.     struct symbol_entry *p;
  140.  
  141.     p = (struct symbol_entry *) xmalloc (sizeof (struct symbol_entry));
  142.  
  143.     p->name = (char *) xmalloc (strlen (name) + 1);
  144.     strcpy (p->name, name);
  145.  
  146.     p->next = NULL;
  147.  
  148.     return p;
  149. }
  150.  
  151. struct header_entry *
  152. cinfo_build_header_entry (char *name)
  153. {
  154.     struct header_entry *p;
  155.  
  156.     p = (struct header_entry *) xmalloc (sizeof (struct header_entry));
  157.  
  158.     p->name = (char *) xmalloc (strlen (name) + 1);
  159.     strcpy (p->name, name);
  160.  
  161.     p->head = NULL;
  162.     p->next = NULL;
  163.  
  164.     return p;
  165. }
  166.  
  167. struct library_entry *
  168. cinfo_build_library_entry (char *name)
  169. {
  170.     struct library_entry *p;
  171.  
  172.     p = (struct library_entry *) xmalloc (sizeof (struct library_entry));
  173.  
  174.     p->name = (char *) xmalloc (strlen (name) + 1);
  175.     strcpy (p->name, name);
  176.  
  177.     p->head = NULL;
  178.     p->next = NULL;
  179.  
  180.     return p;
  181. }
  182.  
  183. void
  184. cinfo_free_library_entry (struct library_entry *entry)
  185. {
  186.     entry->next = NULL;
  187.     entry->head = NULL;
  188.     free (entry->name);
  189.     entry->name = NULL;
  190.     free (entry);
  191. }
  192.  
  193. void
  194. cinfo_free_header_entry (struct header_entry *entry)
  195. {
  196.     entry->next = NULL;
  197.     entry->head = NULL;
  198.     free (entry->name);
  199.     entry->name = NULL;
  200.     free (entry);
  201. }
  202.  
  203. void
  204. cinfo_free_symbol_entry (struct symbol_entry *entry)
  205. {
  206.     entry->next = NULL;
  207.     free (entry->name);
  208.     entry->name = NULL;
  209.     free (entry);
  210. }
  211.  
  212. struct header_entry *
  213. cinfo_add_header_to_list (struct header_entry *list,
  214.               struct header_entry *entry)
  215. {
  216.     struct header_entry *p = list;
  217.  
  218.     while (p->next != NULL)
  219.     p = p->next;
  220.  
  221.     p->next = entry;
  222.  
  223.     return list;
  224. }
  225.  
  226. struct symbol_entry *
  227. cinfo_add_symbol_to_list (struct symbol_entry *list,
  228.               struct symbol_entry *entry)
  229. {
  230.     struct symbol_entry *p = list;
  231.  
  232.     while (p->next != NULL)
  233.     p = p->next;
  234.  
  235.     p->next = entry;
  236.  
  237.     return list;
  238. }
  239.  
  240. struct library_entry *
  241. cinfo_add_library_to_list (struct library_entry *list,
  242.                struct library_entry *entry)
  243. {
  244.     struct library_entry *p = list;
  245.  
  246.     while (p->next != NULL)
  247.     p = p->next;
  248.  
  249.     p->next = entry;
  250.  
  251.     return list;
  252. }
  253.  
  254. struct header_entry *
  255. cinfo_search_header (struct header_entry *list, char *name)
  256. {
  257.     char buf[128];
  258.     struct header_entry *p = list;
  259.  
  260.     while (p != NULL)
  261.     {
  262.     if (strcmp (cinfo_decompact_info_first (p->name, buf), name) == 0)
  263.         return p;
  264.     p = p->next;
  265.     }
  266.  
  267.     return NULL;
  268. }
  269.  
  270. struct symbol_entry *
  271. cinfo_search_symbol (struct symbol_entry *list, char *name)
  272. {
  273.     char buf[128];
  274.     struct symbol_entry *p = list;
  275.  
  276.     while (p != NULL)
  277.     {
  278.     if (strcmp (cinfo_decompact_info_first (p->name, buf), name) == 0)
  279.         return p;
  280.     p = p->next;
  281.     }
  282.  
  283.     return NULL;
  284. }
  285.  
  286. struct library_entry *
  287. cinfo_search_library (struct library_entry *list, char *name)
  288. {
  289.     char buf[128];
  290.     struct library_entry *p = list;
  291.  
  292.     while (p != NULL)
  293.     {
  294.     if (strcmp (cinfo_decompact_info_first (p->name, buf), name) == 0)
  295.         return p;
  296.     p = p->next;
  297.     }
  298.  
  299.     return NULL;
  300. }
  301.  
  302. struct symbol_entry *
  303. cinfo_query_symbol_list_about_symbol (struct symbol_entry *list, char *name)
  304. {
  305.     struct symbol_entry *p = list, *e, *r = NULL;
  306.  
  307.     while (p != NULL && (p = cinfo_search_symbol (p, name)) != NULL)
  308.     {
  309.     e = cinfo_build_symbol_entry (p->name);
  310.  
  311.     if (r == NULL)
  312.         r = e;
  313.     else
  314.         cinfo_add_symbol_to_list (r, e);
  315.  
  316.     p = p->next;
  317.     }
  318.  
  319.     return r;
  320. }
  321.  
  322. struct symbol_entry *
  323. cinfo_duplicate_symbol_list (struct symbol_entry *list)
  324. {
  325.     struct symbol_entry *p = list, *e, *r = NULL;
  326.  
  327.     while (p != NULL)
  328.     {
  329.     e = cinfo_build_symbol_entry (p->name);
  330.  
  331.     if (r == NULL)
  332.         r = e;
  333.     else
  334.         cinfo_add_symbol_to_list (r, e);
  335.  
  336.     p = p->next;
  337.     }
  338.  
  339.     return r;
  340. }
  341.  
  342. struct header_entry *
  343. cinfo_duplicate_header_list (struct header_entry *list)
  344. {
  345.     struct header_entry *p = list, *e, *r = NULL;
  346.  
  347.     while (p != NULL)
  348.     {
  349.     e = cinfo_build_header_entry (p->name);
  350.  
  351.     if (p->head != NULL)
  352.         e->head = cinfo_duplicate_symbol_list (p->head);
  353.  
  354.     if (r == NULL)
  355.         r = e;
  356.     else
  357.         cinfo_add_header_to_list (r, e);
  358.  
  359.     p = p->next;
  360.     }
  361.  
  362.     return r;
  363. }
  364.  
  365. struct library_entry *
  366. cinfo_duplicate_library_list (struct library_entry *list)
  367. {
  368.     struct library_entry *p = list, *e, *r = NULL;
  369.  
  370.     while (p != NULL)
  371.     {
  372.     e = cinfo_build_library_entry (p->name);
  373.  
  374.     if (p->head != NULL)
  375.         e->head = cinfo_duplicate_header_list (p->head);
  376.  
  377.     if (r == NULL)
  378.         r = e;
  379.     else
  380.         cinfo_add_library_to_list (r, e);
  381.  
  382.     p = p->next;
  383.     }
  384.  
  385.     return r;
  386. }
  387.  
  388. struct header_entry *
  389. cinfo_query_header_list_about_header (struct header_entry *list, char *name)
  390. {
  391.     struct header_entry *p = list, *e, *r = NULL;
  392.  
  393.     while (p != NULL && (p = cinfo_search_header (p, name)) != NULL)
  394.     {
  395.     e = cinfo_build_header_entry (p->name);
  396.     e->head = cinfo_duplicate_symbol_list (p->head);
  397.  
  398.     if (r == NULL)
  399.         r = e;
  400.     else
  401.         cinfo_add_header_to_list (r, e);
  402.  
  403.     p = p->next;
  404.     }
  405.  
  406.     return r;
  407. }
  408.  
  409. struct library_entry *
  410. cinfo_query_library_list_about_library (struct library_entry *list, char *name)
  411. {
  412.     struct library_entry *p = list, *e, *r = NULL;
  413.  
  414.     while (p != NULL && (p = cinfo_search_library (p, name)) != NULL)
  415.     {
  416.     e = cinfo_build_library_entry (p->name);
  417.     e->head = cinfo_duplicate_header_list (p->head);
  418.  
  419.     if (r == NULL)
  420.         r = e;
  421.     else
  422.         cinfo_add_library_to_list (r, e);
  423.  
  424.     p = p->next;
  425.     }
  426.  
  427.     return r;
  428. }
  429.  
  430. struct header_entry *
  431. cinfo_query_header_list_about_symbol (struct header_entry *list, char *name)
  432. {
  433.     struct header_entry *hp = list, *he, *r = NULL;
  434.     struct symbol_entry *sp;
  435.  
  436.     while (hp != NULL)
  437.     {
  438.     sp = cinfo_query_symbol_list_about_symbol (hp->head, name);
  439.  
  440.     if (sp != NULL)
  441.     {
  442.         he = cinfo_build_header_entry (hp->name);
  443.         he->head = sp;
  444.  
  445.         if (r == NULL)
  446.         r = he;
  447.         else
  448.         cinfo_add_header_to_list (r, he);
  449.     }
  450.  
  451.     hp = hp->next;
  452.     }
  453.  
  454.     return r;
  455. }
  456.  
  457. struct library_entry *
  458. cinfo_query_library_list_about_header (struct library_entry *list, char *name)
  459. {
  460.     struct library_entry *lp = list, *le, *r = NULL;
  461.     struct header_entry *hp;
  462.  
  463.     while (lp != NULL)
  464.     {
  465.     hp = cinfo_query_header_list_about_header (lp->head, name);
  466.  
  467.     if (hp != NULL)
  468.     {
  469.         le = cinfo_build_library_entry (lp->name);
  470.         le->head = hp;
  471.  
  472.         if (r == NULL)
  473.         r = le;
  474.         else
  475.         cinfo_add_library_to_list (r, le);
  476.     }
  477.  
  478.     lp = lp->next;
  479.     }
  480.  
  481.     return r;
  482. }
  483.  
  484. struct library_entry *
  485. cinfo_query_library_list_about_symbol (struct library_entry *list, char *name)
  486. {
  487.     struct library_entry *lp = list, *le, *r = NULL;
  488.     struct header_entry *hp;
  489.  
  490.     while (lp != NULL)
  491.     {
  492.     hp = cinfo_query_header_list_about_symbol (lp->head, name);
  493.  
  494.     if (hp != NULL)
  495.     {
  496.         le = cinfo_build_library_entry (lp->name);
  497.         le->head = hp;
  498.  
  499.         if (r == NULL)
  500.         r = le;
  501.         else
  502.         cinfo_add_library_to_list (r, le);
  503.     }
  504.  
  505.     lp = lp->next;
  506.     }
  507.  
  508.     return r;
  509. }
  510.  
  511. void
  512. cinfo_explore_symbol_list (struct symbol_entry *list)
  513. {
  514.     struct symbol_entry *p = list;
  515.  
  516.     while (p != NULL)
  517.     {
  518.     printf ("    | symbol %p \"%s\" next -> %p\n",
  519.         p, p->name, p->next);
  520.  
  521.     p = p->next;
  522.     }
  523. }
  524.  
  525. void
  526. cinfo_explore_header_list (struct header_entry *list)
  527. {
  528.     struct header_entry *p = list;
  529.  
  530.     while (p != NULL)
  531.     {
  532.     printf ("  | header %p \"%s\" next -> %p symbols -> %p\n",
  533.         p, p->name, p->next, p->head);
  534.  
  535.     if (p->head != NULL)
  536.         cinfo_explore_symbol_list (p->head);
  537.  
  538.     p = p->next;
  539.     }
  540. }
  541.  
  542. void
  543. cinfo_explore_library_list (struct library_entry *list)
  544. {
  545.     struct library_entry *p = list;
  546.  
  547.     while (p != NULL)
  548.     {
  549.     printf ("library %p \"%s\" next -> %p headers -> %p\n",
  550.         p, p->name, p->next, p->head);
  551.  
  552.     if (p->head != NULL)
  553.         cinfo_explore_header_list (p->head);
  554.  
  555.     p = p->next;
  556.     }
  557. }
  558.  
  559. void
  560. cinfo_recursive_free_symbol_list (struct symbol_entry *list)
  561. {
  562.     struct symbol_entry *p = list, *next;
  563.  
  564.     while (p != NULL)
  565.     {
  566.     next = p->next;
  567.  
  568.     cinfo_free_symbol_entry (p);
  569.  
  570.     p = next;
  571.     }
  572. }
  573.  
  574. void
  575. cinfo_recursive_free_header_list (struct header_entry *list)
  576. {
  577.     struct header_entry *p = list, *next;
  578.  
  579.     while (p != NULL)
  580.     {
  581.     if (p->head != NULL)
  582.         cinfo_recursive_free_symbol_list (p->head);
  583.  
  584.     next = p->next;
  585.  
  586.     cinfo_free_header_entry (p);
  587.  
  588.     p = next;
  589.     }
  590. }
  591.  
  592. void
  593. cinfo_recursive_free_library_list (struct library_entry *list)
  594. {
  595.     struct library_entry *p = list, *next;
  596.  
  597.     while (p != NULL)
  598.     {
  599.     if (p->head != NULL)
  600.         cinfo_recursive_free_header_list (p->head);
  601.  
  602.     next = p->next;
  603.  
  604.     cinfo_free_library_entry (p);
  605.  
  606.     p = next;
  607.     }
  608. }
  609.  
  610. /*
  611.    avrebbe dovuto essere una cosa semplice semplice...
  612.    ...ma in poco tempo diveni' una porcheria. --S.S.
  613. */
  614.